package com.hctg.forced2sleep.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.FrameLayout;

import com.actor.myandroidframework.utils.ConfigUtils;

import java.util.List;

/**
 * description: 描述
 * company    :
 *
 * @author : ldf
 * date       : 2024/10/6 on 15
 * @version 1.0
 */
public class AccessibilityNodesView extends FrameLayout {

    protected final Paint paint = new Paint();
    protected final int grayTrans = Color.parseColor("#55000000");
    protected final int deepGrayTrans = Color.parseColor("#aa000000");
    protected final int red = Color.parseColor("#FF3C3C");
    protected final int pink = Color.parseColor("#88FF3C3C");
    private GestureDetector gestureDetector;
    protected Rect heightLightRect;
    protected List<AccessibilityNodeInfo> nodesInfo;
    protected boolean             hasStatusBar = true;
    protected OnRectClickListener rectClickListener;

    private class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onSingleTapConfirmed(MotionEvent motionEvent) {
            if (nodesInfo == null || nodesInfo.isEmpty()) {
                return super.onSingleTapConfirmed(motionEvent);
            }
            Rect rectChild = null;
            Rect rect = new Rect();
            float rawX = motionEvent.getRawX();
            float rawY = motionEvent.getRawY();
//            LogUtils.errorFormat("x=%f, y=%f", motionEvent.getX(), motionEvent.getY());
//            LogUtils.errorFormat("RawX=%f, RawY=%f", rawX, rawY);
            for (AccessibilityNodeInfo nodeInfo : nodesInfo) {
                nodeInfo.getBoundsInScreen(rect);
                //if当前节点被点击
                if (rect.contains((int) rawX, (int) rawY)) {
                    if (rectChild == null) {
                        rectChild = new Rect(rect);
                    } else if (rectChild.contains(rect)) {
                        rectChild.set(rect);
//                        rectChild = rect;
                    }
                }
            }
            heightLightRect = rectChild;
            invalidate();
            if (rectChild != null) {
                if (rectClickListener != null && !nodesInfo.isEmpty()) {
                    Rect rectRoot = new Rect();
                    nodesInfo.get(0).getBoundsInScreen(rectRoot);
                    rectClickListener.onReckClick(rectRoot, rectChild, hasStatusBar);
                }
                return true;
            }
            return super.onSingleTapConfirmed(motionEvent);
        }
    }

    public AccessibilityNodesView(Context context) {
        super(context);
        init(context, null);
    }

    public AccessibilityNodesView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public AccessibilityNodesView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    public AccessibilityNodesView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs);
    }

    protected void init(Context context, AttributeSet attrs) {
        setClickable(true); //确保可点击, 否则点击了无反应
        paint.setAntiAlias(true);
//        paint.setAlpha(0);  //透明
//        paint.setDither(true);
        paint.setStyle(Paint.Style.STROKE); //设置画笔样式：描边
        paint.setStrokeJoin(Paint.Join.ROUND);  //设置线段连接处样式: 结合处为圆弧

        this.gestureDetector = new GestureDetector(context, new MyGestureListener());
    }

    public interface OnRectClickListener {
        public void onReckClick(Rect rectRoot, Rect rect, boolean hasStatusBar);
    }
    public void setOnRectClickListener(OnRectClickListener rectClickListener) {
        this.rectClickListener = rectClickListener;
    }

    public void setPageInfos(boolean hasStatusBar, List<AccessibilityNodeInfo> nodesInfo) {
        this.hasStatusBar = hasStatusBar;
        this.nodesInfo = nodesInfo;
        this.heightLightRect = null;
        postInvalidate();
    }

    @Override
    public void dispatchDraw(Canvas canvas) {
        if (nodesInfo == null || nodesInfo.isEmpty()) {
            super.dispatchDraw(canvas);
            return;
        }
        canvas.drawColor(grayTrans);
        paint.setColor(red);
        Rect rect = new Rect();
        for (AccessibilityNodeInfo nodeInfo : nodesInfo) {
            nodeInfo.getBoundsInScreen(rect);
            if (hasStatusBar) {
                rect.top -= ConfigUtils.STATUS_BAR_HEIGHT;
                rect.bottom -= ConfigUtils.STATUS_BAR_HEIGHT;
            }
            canvas.drawRect(rect, paint);
        }
        //
        if (heightLightRect != null) {
            rect.set(heightLightRect);
            //背景更加灰暗
//            canvas.drawColor(deepGrayTrans);
//            Paint paint = new Paint();
//            paint.setAlpha(0);
//            paint.setDither(true);
            paint.setStyle(Paint.Style.FILL);
//            paint.setStrokeJoin(Paint.Join.ROUND);
//            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
//            canvas.drawRect(heightLightRect, paint);
//            paint.setAlpha(1);
//            paint.setXfermode(null);
            paint.setColor(pink);
            if (hasStatusBar) {
                rect.top -= ConfigUtils.STATUS_BAR_HEIGHT;
                rect.bottom -= ConfigUtils.STATUS_BAR_HEIGHT;
            }
            canvas.drawRect(rect, paint);

            paint.setColor(red);
            paint.setStyle(Paint.Style.STROKE);
        }
        super.dispatchDraw(canvas);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            performClick();
        }
        if (gestureDetector.onTouchEvent(event)) {
            return true;
        }
        return super.onTouchEvent(event);
    }

    @Override
    public boolean performClick() {
        return super.performClick();
    }

    @Override
    public CharSequence getAccessibilityClassName() {
        return CantGetAccessibilityAble.class.getName();
    }
}
